<template>
  <div 
    v-if="visible" 
    class="image-preview-modal"
    @click.self="handleClose"
  >
    <!-- 遮罩层 -->
    <div class="modal-backdrop" :class="{ 'fade-in': visible, 'fade-out': !visible }"></div>
    
    <!-- 弹窗内容 -->
    <div class="modal-container" :class="{ 'slide-in': visible, 'slide-out': !visible }">
      <!-- 关闭按钮 -->
      <button class="close-btn" @click="handleClose" aria-label="关闭">
        <el-icon><Close /></el-icon>
      </button>
      
      <div class="modal-content">
        <!-- 左侧大图预览区 -->
        <div class="preview-area">
          <div class="image-container">
            <!-- 加载状态 -->
            <!-- 主图片 -->
            <img 
              :src="currentImage.url" 
              :alt="currentImage.alt || '图片预览'" 
              class="main-image"
              @load="isLoading = false"
              @error="handleImageError"
            >
          </div>
          
          <!-- 图片导航信息 -->
          <div class="image-info">
            <span class="current-index">{{ currentIndex + 1 }} / {{ images.length }}</span>
          </div>
        </div>
        
        <!-- 右侧缩略图总览区 -->
        <div class="thumbnails-area">
          <h3 class="thumbnails-title">所有图片</h3>
          <div @click="handleKeyDown2('ArrowLeft')" style="display: flex;align-items: center;justify-content: center;height: 50px;cursor: pointer;"><el-icon style="color: #fff;"><ArrowUpBold /></el-icon></div>
          <div class="thumbnails-list">
            <div 
              v-for="(img, idx) in images" 
              :key="idx"
              class="thumbnail-item"
              :class="{ 'active': idx === currentIndex }"
              @click.stop="setCurrentIndex(idx)"
            >
              <img 
                :src="img.thumbnailUrl || img.url" 
                :alt="img.alt || `缩略图 ${idx + 1}`"
                class="thumbnail-image"
              >
            </div>
          </div>
          <div  @click="handleKeyDown2('ArrowRight')" style="display: flex;align-items: center;justify-content: center;height: 50px;cursor: pointer;"><el-icon style="color: #fff;"><ArrowDownBold /></el-icon></div>

        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onMounted, onUnmounted, computed } from 'vue'; 

import {Close,ArrowUpBold,ArrowDownBold} from '@element-plus/icons-vue';
// 图片类型定义
interface ImageItem {
  url?: string; // 大图地址
  thumbnailUrl?: string; // 缩略图地址，可选
  alt?: string; // 图片描述
}

// 组件 props
const props = defineProps<{
  visible: boolean;
  images: ImageItem[];
  initialIndex?: number;
  onClose: () => void;
}>();

// 组件状态
const currentIndex = ref(0);
const isLoading = ref(true);

// 计算当前显示的图片
// 修改 computed 属性部分如下：
const currentImage = computed<ImageItem>(() => {
  const img = props.images[currentIndex.value] || props.images[0];
  return img ?? { thumbnailUrl: '', alt: '暂无图片' }; // 提供默认 fallback 防止 undefined
});
 

// 验证初始索引
const validateIndex = (index: number): number => {
  if (props.images.length === 0) return 0;
  return Math.max(0, Math.min(index, props.images.length - 1));
};

// 设置当前索引并加载图片
const setCurrentIndex = (index: number) => {
  if (index === currentIndex.value) return;
  
  currentIndex.value = validateIndex(index);
  isLoading.value = true; // 触发加载状态
};

// 处理图片加载错误
const handleImageError = () => {
  isLoading.value = false;
  // 可以在这里设置错误图片占位符
};

// 处理关闭事件
const handleClose = () => {
  props.onClose();
};

// 键盘导航
const handleKeyDown = (e: KeyboardEvent) => {
  if (!props.visible) return;
  
  // 左箭头或上箭头 - 上一张
  if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
    e.preventDefault();
    setCurrentIndex(currentIndex.value - 1);
  }
  
  // 右箭头或下箭头 - 下一张
  if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
    e.preventDefault();
    setCurrentIndex(currentIndex.value + 1);
  }
  
  // ESC键 - 关闭
  if (e.key === 'Escape') {
    e.preventDefault();
    handleClose();
  }
};
const handleKeyDown2 = (e: string) => {
  if (!props.visible) return;
  
  // 左箭头或上箭头 - 上一张
  if (e === 'ArrowLeft') {
    setCurrentIndex(currentIndex.value - 1);
  }
  // 右箭头或下箭头 - 下一张
  if (e === 'ArrowRight') {
    setCurrentIndex(currentIndex.value + 1);
  }

};

// 监听props变化
watch(
  () => props.initialIndex,
  (newVal) => {
    if (newVal !== undefined) {
      currentIndex.value = validateIndex(newVal);
    }
  },
  { immediate: true }
);

watch(
  () => props.images,
  () => {
    currentIndex.value = validateIndex(currentIndex.value);
    isLoading.value = true;
  }
);

// 监听visible变化重置加载状态
watch(
  () => props.visible,
  (newVal) => {
    if (newVal) {
      isLoading.value = true;
    }
  }
);

// 注册键盘事件
onMounted(() => {
  window.addEventListener('keydown', handleKeyDown);
});

// 移除键盘事件
onUnmounted(() => {
  window.removeEventListener('keydown', handleKeyDown);
});
</script>

<style scoped>
/* 基础样式 */
.image-preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1000;
  overflow: hidden;
}

/* 遮罩层样式 */
.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* 弹窗容器样式 */
.modal-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: translateY(20px);
  opacity: 0;
  transition: all 0.3s ease;
}

/* 动画类 */
.fade-in {
  opacity: 1;
}

.fade-out {
  opacity: 0;
}

.slide-in {
  transform: translateY(0);
  opacity: 1;
}

.slide-out {
  transform: translateY(20px);
  opacity: 0;
}

/* 关闭按钮 */
.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  z-index: 10;
}

.close-btn:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.icon-close::before {
  content: "✕";
  font-size: 20px;
}

/* 弹窗内容区域 */
.modal-content {
  display: flex;
  width: 90%;
  height: 90%;
  background: #2C2C2C;
  max-width: 1400px;
  gap: 20px;
}

/* 左侧预览区 */
.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.image-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.main-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transition: opacity 0.3s ease;
  border-radius: 4px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* 加载状态 */
.image-loading {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(255, 255, 255, 0.1);
  border-top-color: #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 图片信息 */
.image-info {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.current-index {
  background-color: rgba(0, 0, 0, 0.5);
  padding: 4px 8px;
  border-radius: 12px;
}

.image-name {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  max-width: 400px;
}

/* 右侧缩略图区域 */
.thumbnails-area {
  width: 220px;
  display: flex;
  flex-direction: column;
  background-color: rgba(25, 25, 25, 0.8);
  border-radius: 8px;
  padding: 15px;
  overflow: hidden;
  box-shadow: -2px 0 10px rgba(0, 0, 0, 0.2);
}

.thumbnails-title {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin: 0 0 15px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.thumbnails-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 5px;
  height: 600px;
}

/* 自定义滚动条 */
.thumbnails-list::-webkit-scrollbar {
  width: 6px;
}

.thumbnails-list::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
}

.thumbnails-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.thumbnails-list::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 缩略图项 */
.thumbnail-item {
  width: 100%;
  height: 100px;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  transition: all 0.2s ease;
  border: 2px solid transparent;
}

.thumbnail-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.thumbnail-item.active {
  border-color: #4096ff;
}

.thumbnail-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .modal-content {
    flex-direction: column;
    height: auto;
    max-height: 90vh;
  }
  
  .preview-area {
    height: 60vh;
  }
  
  .thumbnails-area {
    width: 100%;
    height: 200px;
  }
  
  .thumbnails-list {
    flex-direction: row;
    overflow-x: auto;
    padding-bottom: 5px;
  }
  
  .thumbnail-item {
    min-width: 120px;
    height: 120px;
  }
}

@media (max-width: 576px) {
  .image-info {
    flex-direction: column;
    gap: 5px;
    align-items: center;
  }
  
  .thumbnail-item {
    min-width: 80px;
    height: 80px;
  }
  
  .close-btn {
    width: 34px;
    height: 34px;
  }
}
</style>
